package org.yibinxueyuan.drawcard.controller;

import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.yibinxueyuan.drawcard.entity.Cardinfo;
import org.yibinxueyuan.drawcard.pojo.Paging;
import org.yibinxueyuan.drawcard.pojo.cardinfosAndPaging;
import org.yibinxueyuan.drawcard.service.CardInfoService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 卡片管理页面的控制 json数据交互
 */
@RestController
@RequestMapping("/ajax")
public class CardAjaxController  extends ApplicationObjectSupport {
    @Resource(name = "CardinfoServiceImpl")
    private CardInfoService cardInfoService;

    @Resource(name = "pagingBean")
    private Paging pag;


    @RequestMapping(value = "/cardmanage")
    public cardinfosAndPaging cardManage(Paging paging,HttpServletRequest request){
        List<Cardinfo> cardinfos = null;
        int number = -1;
        //设置message提示信息
        String message = request.getParameter("message");

        //根据卡片名 查询相应的总数据数 如果为空,就查询所有卡片数据数
        number = cardInfoService.queryAllTotalCardNumber(paging.getCardName());
        paging.setTotalNumber(number);

        //根据页数查询相应页数的数据,
        cardinfos = cardInfoService.queryAllCardByPage(paging);

        /**
         *  将数据加入到cardPaging中
         */
        cardinfosAndPaging cardPaging = (cardinfosAndPaging) this.getApplicationContext().getBean("getCardinfosAndPagingBean");
        cardPaging.setCardinfos(cardinfos);
        cardPaging.setPaging(paging);
        cardPaging.setMessage(message);

        return cardPaging;
    }



    /**
     * 增加卡片的业务处理(json)
     * @param cardinfo
     *
     * @return
     */
    @RequestMapping(value = "/card/insert")
    public cardinfosAndPaging insertCard(Cardinfo cardinfo){
        cardinfosAndPaging cardPaging = (cardinfosAndPaging) this.getApplicationContext().getBean("getCardinfosAndPagingBean");
        String message = null;
        int number = cardInfoService.insertNewCard(cardinfo);
        if(number >= 1){
            message="增加成功,请继续接下来的操作";
        }else {
            message="增加失败,请检查数据后重试";
        }
        cardPaging.setMessage(message);
        return cardPaging;
    }

    /**
     * 更新卡片的业务逻辑(json)
     */
    @RequestMapping(value = "card/update")
    public cardinfosAndPaging updateCard(Cardinfo cardinfo){
        cardinfosAndPaging cardPaging = (cardinfosAndPaging) this.getApplicationContext().getBean("getCardinfosAndPagingBean");
        String message = null;
        int number = cardInfoService.updateCardinfoById(cardinfo);
        if(number>=1){
            message = "修改成功,请继续其他操作";
        }else{
            message="修改失败,请检查数据后重试";
        }
        cardPaging.setMessage(message);
        return cardPaging;
    }

    /**
     * 删除卡片的业务逻辑
     */
    @RequestMapping(value = "/card/delete")
    public cardinfosAndPaging deleteCard(int cardId){
        cardinfosAndPaging cardPaging = (cardinfosAndPaging) this.getApplicationContext().getBean("getCardinfosAndPagingBean");
        String message = null;
        int number = cardInfoService.deleteCardinfoById(cardId);
        if(number>=1){
            message = "删除成功,请继续其他操作";
        }else{
            message = "删除失败,请重试";
        }
        cardPaging.setMessage(message);
        return cardPaging;
    }
}
